<template>
  <div class="add">
    <header>
      <div class="left">添加部门</div>
      <div class="right">
        <a><i class="el-icon-minus"></i></a>
        <a><i class="el-icon-copy-document"></i></a>
        <a @click="cancel"><i class="el-icon-close"></i></a>
      </div>
    </header>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="部门名称" prop="shortName">
        <el-input type="text" v-model="ruleForm.shortName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orders">
        <el-input type="text" v-model="ruleForm.orders" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="部门全称" prop="fullName">
        <el-input type="text" v-model="ruleForm.fullName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="上级部门" prop="upone">
        <el-input type="text" v-model="ruleForm.upone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input type="text" v-model="ruleForm.notes" autocomplete="off"></el-input>
      </el-form-item>
      <div class="footer">
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" :loading="isLoading">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script type="text/ecmascript-6">
import { addMember } from "@/api/request.js";
export default {
  name: "",
  data() {
    var validateshortName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("部门名称不能为空"));
      } else {
        callback();
      }
    };
    var validatefullName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("部门全称不能为空"));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        shortName: "",
        fullName: "",
        orders: ""
      },
      rules: {
        shortName: [{ validator: validateshortName, trigger: "blur" }],
        fullName: [{ validator: validatefullName, trigger: "blur" }]
      },
      //   加载中状态
      isLoading: false
    };
  },
  components: {},
  methods: {
    submitForm(formName) {
      this.isLoading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          //发起请求，验证身份
          addMember(this.ruleForm)
            .then(res => {
              this.isLoading = false;
              console.log(res.data)
            })
            .catch(err => {
              console.log("err", err);
            });
        } else {
          this.$message.error("提交失败");
          return false;
        }
      });
    },
    cancel() {
      this.$emit("cancel");
    }
  }
};
</script>

<style lang="stylus" scoped>
.el-form {
  position: relative;
  height: 236px;
  margin: 15px;
  padding: 15px 0;
  font: 13px;
  background: #fff;
  overflow: hidden;
}

.add {
  box-sizing: border-box;
  position: fixed;
  width: 780px;
  height: 320px;
  left: 50%;
  top: 50%;
  margin-left: -390px;
  margin-top: -160px;
  background-color: #fff;
  background: #EDECEC;
}

header {
  position: relative;
  height: 40px;
  background: #F8F8F8;
  line-height: 40px;
  border-bottom: 1px solid #eee;
  border-radius: 2px 2px 0 0;
  overflow: hidden;
}

header .left {
  float: left;
  font: 14px;
  color: #333;
  padding: 0 80px 0 20px;
}

header .right {
  float: right;
  position: absolute;
  right: 15px;
  top: 0;
}
header .right a{
  margin 0 4px
}
.el-form-item {
  width: 373px;
  height: 50px;
  margin: 0;
  float: left;
}

.footer {
  position: absolute;
  left: -70px;
  bottom: 15px;
}
</style>

